/***************** Modification History ***********************************
   @doc
   @module revutl.c - Functions to Control Reversal processing |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Comm/revutl.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>
#define VSREVUTL_C

#include <stdio.h>


#ifdef DEBUG_vsrevutl
#define DEBUG_95
#endif
#include <string.h>
#include <define.h>
#include <msg.h>
#include <hdt.h>
#include <tblutil.h>
#include <mit.h>
#include <revutl.h>
#include <spdebug.h>
#include <codesec.h>

/*-------------------------------------------------------------------------
    Function :     vdGetReversalFileName
    Description :  This routine gets the name of the Reversal File file for
                   all hosts.
    Parameters :   Pointer to the file name character array.
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09/20/95  Henry_G1   Replaced hard filename with the host name.
--------------------------------------------------------------------------*/

void vdGetReversalFileName(char* pszFName,int inHostIndex) {
    sprintf(pszFName, "ISO%02d%02d", inHostIndex, inGetMerchantNum());
}


int inRenameReversalFile(void) {
    char szSourceReversalFileName[20];
    char szDestReversalFielName[20];

    vdGetReversalFileName(szSourceReversalFileName, inGetHostIndex());
    sprintf(szDestReversalFielName, "%s.%s", szSourceReversalFileName, "rev");
    strcat(szSourceReversalFileName, BACKUP_REV_FILE_EXTN);
    file_copy(szSourceReversalFileName, szDestReversalFielName);
    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    fReversalPendingAtHost
    Description : Checks if there is any reversal pending for a particular
                  host.
    Parameters :  Host Index
    Returns :     VS_TRUE  if there is a reversal pending
                  VS_FALSE If there is no reversal pending.
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------

--------------------------------------------------------------------------*/

VS_BOOL fReversalPendingAtHost(int inHostIndex) {
    int inFlHandle;
    char szReversalFile[FILENAME_SIZE + 1];

    pdebug(("--fReversalPendingAtHost--"));

    vdGetReversalFileName(szReversalFile, inHostIndex);
    strcat(szReversalFile, REVERSAL_FILE_EXTN);

    /* Return VS_SUCCESS if reversal is pending */
    if ((inFlHandle = open(szReversalFile, O_RDONLY)) < 0)
        return(VS_FALSE);
    if (close(inFlHandle) < 0)
        return(VS_FALSE);

    return(VS_TRUE);
}

/*-------------------------------------------------------------------------
    Function :     inReversalPending
    Description :
    Parameters :
    Returns :      First host index with a pending reversal,
                   VS_ERR if no reversals pending.
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   07/21/95  Henry_G1   Created.
--------------------------------------------------------------------------*/

int inReversalPending() {
    int inHostIndex;
    int inNoHDTRecs;

    inNoHDTRecs = inGetHDTTotRecs();

    for (inHostIndex = 0; inHostIndex < inNoHDTRecs; inHostIndex++)
        if (fReversalPendingAtHost(inHostIndex))
            return(inHostIndex);

    return(VS_ERR);
}

#ifdef Page_K1
/*-------------------------------------------------------------------------
    Function :    inRetrieveReversalRequest
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   07/13/95  Henry_G1   Created.
--------------------------------------------------------------------------*/

int inRetrieveReversalRequest(char* szReversalFile,char* szRequestBuffer,int inRequestBufferSize) {
    int inFlHandle;
    int inRecSize;

    pdebug(("--inRetrieveReversalRequest--"));
    pdebug(("szReversalFile = %s", szReversalFile));

    /* Get the reversal record */
    if ((inFlHandle = open(szReversalFile, O_RDONLY)) < 0)
        return(VS_ERR);

    if ((inRecSize = read(inFlHandle, szRequestBuffer, inRequestBufferSize)) <= 0) {
        close(inFlHandle);
        return(VS_ERR);
    }

    if (close(inFlHandle) < 0)
        return(VS_ERR);

    return(inRecSize);
}
#endif

int inSaveReversalRequest(char* szRequestBuffer,int inRequestBufferSize,int inHostIndex) {
    int inFlHandle;
    char szReversalFile[FILENAME_SIZE + 1];

    pdebug(("--inSaveReversalRequest--"));

    vdGetReversalFileName(szReversalFile, inHostIndex);
    strcat(szReversalFile, REVERSAL_FILE_EXTN);
    pdebug(("szReversalFile = %s", szReversalFile));

    /* Save the reversal record for this host in the reversal file*/
    if ((inFlHandle = open(szReversalFile, O_RDWR | O_CREAT)) < 0)
        return(VS_ERR);

    if ((write(inFlHandle, szRequestBuffer, inRequestBufferSize)) <= 0) {
        close(inFlHandle);
        return(VS_ERR);
    }

    if (close(inFlHandle) < 0)
        return(VS_ERR);

    return(VS_SUCCESS);
}

void vdCancelPendingReversal(int inHostIndex) {
    char szReversalFile[FILENAME_SIZE + 1];

    pdebug(("--vdCancelPendingReversal--"));

    vdGetReversalFileName(szReversalFile, inHostIndex);
    strcat(szReversalFile, REVERSAL_FILE_EXTN);

    pdebug(("szReversalFile = %s", szReversalFile));
    remove(szReversalFile);
}

int REVUTL_CODE_CHECK(char* a,char* b,short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}

#undef VSREVUTL_C

